<template>
  <div class="tag-wrap">
    <div v-if="tagList">
      <div class="tag-info">
        <div
          v-for="item in tagList"
          :key="'tag-key-' + item.id"
          class="tag-item"
          :class="{ 'active-tag': currentTagId === item.id }"
          @click="getTagArticle(item.id)"
        >
          <span>{{ item.name }}</span>
          <span class="article-total">({{ item.article_total }})</span>
        </div>
      </div>
      <SkeletonCpt v-if="hasMore && !articleList.length" />
      <article
        v-for="item in articleList"
        :key="'article-key-' + item.id"
        class="article-item"
      >
        <div class="article-left">
          <router-link
            :to="`/article/${item.id}`"
            class="head-img"
          >
            <img
              v-if="item['head_img']"
              v-lazy="item['head_img']"
            >
            <NoHeadImgCpt v-else />
          </router-link>
        </div>
        <div class="article-right">
          <router-link
            :to="'/article/' + item.id"
            class="singleEllipsis"
          >
            <h2>{{ item.title }}</h2>
          </router-link>
          <el-divider />
          <el-tag
            v-for="tagItem in item.tags"
            :key="'article-tag-key-' + tagItem.id"
            class="overwrite-el-tag"
            :disable-transitions="false"
            :color="tagItem.color"
          >
            {{ tagItem.name }}
          </el-tag>
          <div class="summary">
            <img
              class="user-avatar"
              :src="item.users[0] && item.users[0].avatar"
              alt=""
            >
            <span class="jgh" />
            <span>{{ $filters.convertDate( item.created_at) }}</span>
            <span class="jgh" />
            <span>{{ item.click }}浏览</span>
            <span class="jgh" />
            <span>{{ item.comment_total }}评论</span>
            <span class="jgh" />
            <div>{{ item.star_total }}star</div>
          </div>
        </div>
      </article>
      <div
        v-if="total"
        class="page-btn"
      >
        <div>
          <el-button
            v-show="articleListParams && articleListParams.nowPage !== 1"
            @click="handlePage('prev')"
          >
            上一页
          </el-button>
        </div>
        <div>
          <el-button
            v-show="hasMore"
            @click="handlePage('next')"
          >
            下一页
          </el-button>
        </div>
      </div>
      <div
        v-else
        class="no-data"
      >
        {{ currentTagName }}标签下暂无文章~
      </div>
    </div>
    <div
      v-else
      class="no-data"
    >
      暂无标签~
    </div>
  </div>
</template>

<script lang="ts">
import NoHeadImgCpt from '@/components/NoHeadImg/index.vue'
import SkeletonCpt from '@/components/Skeleton.vue'
import { defineComponent, inject, reactive, toRefs, onBeforeMount, onMounted, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { InitData } from '@/types/tag'
export default defineComponent({
  name: 'TagView',
  components: { NoHeadImgCpt, SkeletonCpt },
  setup () {
    const $get:any = inject('$get')

    const router = useRouter()
    const route = useRoute()

    const data = reactive(new InitData())

    watch(() => route.params.id, (tagId) => {
      data.currentTagId = ~~tagId

      data.currentTagId && handlePage()
    })

    onBeforeMount(async () => {
      data.tagList = (await $get('tag/list', data.tagListParams)).rows
      data.currentTagId = ~~route.params.id || 1
      await handlePage()
    })

    // 页面加载时
    onMounted(() => {
      console.log('%c页面加载耗时：13ms | Theme By Joe', 'color:#fff; background: linear-gradient(270deg, #986fee, #8695e6, #68b7dd, #18d7d3); padding: 8px 15px; border-radius: 0 15px 0 15px')
    })

    // 获取当前标签下的文章
    const handlePage = async (type:string = '') => {
      if (type === 'prev') {
        data.articleListParams.nowPage--
      } else if (type === 'next') {
        data.articleListParams.nowPage++
      } else {
        data.articleListParams.nowPage = 1
        let title = ''
        data.tagList?.forEach((item) => {
          if (item.id === data.currentTagId) {
            title = item.name
          }
        })
        document.title = `标签 - ${title} - 小锅锅·博客`
      }

      try {
        const articleData = await $get(`tag/article_list/${data.currentTagId}`,
          data.articleListParams
        )
        data.articleList = articleData.rows
        data.total = articleData.total
        data.hasMore = articleData.hasMore
        scrollTo({ top: 0 })
      } catch (error) {
        data.articleList = []
        data.total = 0
        data.articleListParams.nowPage = 1
        console.log(error)
      }
    }

    const getTagArticle = (id:number) => {
      router.push({ path: `/tag/${id}` })
    }

    const dataRef = toRefs(data)
    return {
      ...dataRef,
      handlePage,
      getTagArticle
    }
  }
})
</script>

<style lang="scss" scoped>
@import '@/assets/css/constant.scss';

/* 响应式布局 - 小于 540px */
@media screen and (max-width: 540px) {
  .tag-info {
    font-size: 13px;
    .tag-item {
      margin: 8px 6px !important;
    }
  }
  .article-item {
    flex-wrap: wrap;

    .article-left {
      width: 100%;
      flex: auto !important;
    }
    .article-right {
      padding: 10px 20px !important;
    }
  }
}
.tag-wrap {
  .tag-info {
    .tag-item {
      position: relative;
      display: inline-flex;
      align-items: center;
      justify-content: space-between;
      margin: 10px;
      padding: 0 10px;
      height: 30px;
      border: 1px solid rgba(93, 121, 148, 0.15);
      border-radius: 3px;
      background-color: white;

      &:hover {
        cursor: pointer;
      }
      &::after {
        position: absolute;
        top: 50%;
        left: -1px;
        width: 8px;
        height: 8px;
        border-width: 0 0 1px 1px;
        border-style: solid;
        border-color: inherit;
        background-color: inherit;
        content: '';
        transform: translateX(-50%) translateY(-50%) rotate(45deg);
      }
      .article-total {
        margin-left: 6px;
      }
      &.active-tag {
        border: 1px solid #005cc5 !important;
      }
    }
  }
  .no-data {
    padding: 40px 0;
    text-align: center;
  }
}
.page-btn {
  display: flex;
  justify-content: space-between;
}

.article-item {
  display: flex;
  margin-bottom: 15px;
  width: 100%;
  border-radius: 5px;
  background: white;
  box-shadow: 0 1px 5px rgba(0, 0, 0, 0.05);
  .article-left {
    overflow: hidden;
    flex: 0 0 40%;
    margin: 20px;
    .head-img {
      width: 100%;
      height: 100%;
      background-position: 50% 50%;
      background-size: cover;
      background-repeat: no-repeat;
      cursor: pointer;
      transition: all 0.3s ease 0s;
      &:hover {
        transform: scale(1.1);
      }
    }
  }
  .article-right {
    overflow: hidden;
    box-sizing: border-box;
    padding: 30px;
    .summary {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin: 10px 0;
      font-size: 12px;
      .user-avatar {
        width: 20px;
        height: 20px;
        border-radius: 50%;
      }
    }
  }
}

.jgh::after {
  padding: 0 5px;
  content: '·';
  font-weight: 600;
}

.overwrite-el-tag {
  color: white;
  margin-right: 10px;
}
</style>
